{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "1nQ_rskY0eBd"
   },
   "source": [
    "# **If you are using Colab for free, we highly recommend you active the T4 GPU hardware accelerator. Our models are designed to run with at least 16GB of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as opposed to the 13GB Colab gives automatically.**\n",
    "# **To active T4:**\n",
    "# **1.   click on the \"Runtime\" tab**\n",
    "# **2.   click on \"Change runtime type\"**\n",
    "# **3.   select T4 GPU under Hardware Accelerator**\n",
    "# **NOTE: there is a weekly usage limit on using T4 for free**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "4vZI1vS6wAO5",
    "outputId": "83abeb6a-7415-4a5b-c98f-43123b0740dd"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting llmware\n",
      "  Downloading llmware-0.3.0-py3-none-any.whl (56.0 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m56.0/56.0 MB\u001b[0m \u001b[31m15.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting boto3>=1.24.53 (from llmware)\n",
      "  Downloading boto3-1.34.124-py3-none-any.whl (139 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m139.2/139.2 kB\u001b[0m \u001b[31m2.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: huggingface-hub>=0.19.4 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.23.2)\n",
      "Requirement already satisfied: numpy>=1.23.2 in /usr/local/lib/python3.10/dist-packages (from llmware) (1.25.2)\n",
      "Collecting pymongo>=4.7.0 (from llmware)\n",
      "  Downloading pymongo-4.7.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (669 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m669.1/669.1 kB\u001b[0m \u001b[31m11.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hRequirement already satisfied: tokenizers>=0.15.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.19.1)\n",
      "Collecting psycopg-binary==3.1.17 (from llmware)\n",
      "  Downloading psycopg_binary-3.1.17-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.3/3.3 MB\u001b[0m \u001b[31m52.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting psycopg==3.1.17 (from llmware)\n",
      "  Downloading psycopg-3.1.17-py3-none-any.whl (178 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m178.0/178.0 kB\u001b[0m \u001b[31m16.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
      "\u001b[?25hCollecting pgvector==0.2.4 (from llmware)\n",
      "  Downloading pgvector-0.2.4-py2.py3-none-any.whl (9.6 kB)\n",
      "Collecting colorama==0.4.6 (from llmware)\n",
      "  Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)\n",
      "Requirement already satisfied: librosa>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from llmware) (0.10.2.post1)\n",
      "Requirement already satisfied: typing-extensions>=4.1 in /usr/local/lib/python3.10/dist-packages (from psycopg==3.1.17->llmware) (4.12.1)\n",
      "\u001b[31mERROR: Operation cancelled by user\u001b[0m\u001b[31m\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "!pip install llmware"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 385
    },
    "id": "fNcIsJ7Ewn-S",
    "outputId": "37e8883e-8a18-481e-d956-2e342cf4309f"
   },
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "errorDetails": {
      "actions": [
       {
        "action": "open_url",
        "actionText": "Open Examples",
        "url": "/notebooks/snippets/importing_libraries.ipynb"
       }
      ]
     },
     "evalue": "No module named 'llmware'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-2-3c1ce234e0d9>\u001b[0m in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mllmware\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetup\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mSetup\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mllmware\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlibrary\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mLibrary\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mllmware\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprompts\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mPrompt\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mHumanInTheLoop\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mllmware\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mretrieval\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mQuery\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'llmware'",
      "",
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0;32m\nNOTE: If your import is failing due to a missing package, you can\nmanually install dependencies using either !pip or !apt.\n\nTo view examples of installing some common dependencies, click the\n\"Open Examples\" button below.\n\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from llmware.setup import Setup\n",
    "from llmware.library import Library\n",
    "from llmware.prompts import Prompt, HumanInTheLoop\n",
    "from llmware.retrieval import Query\n",
    "from llmware.configs import LLMWareConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "yXk6A_9Zysmv"
   },
   "outputs": [],
   "source": [
    "LLMWareConfig().set_active_db(\"sqlite\")\n",
    "llm = \"llmware/dragon-yi-6b-gguf\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "qB6uxOZdywo4"
   },
   "outputs": [],
   "source": [
    "local_path = Setup().load_sample_files()\n",
    "agreements_path = os.path.join(local_path, \"AgreementsLarge\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "YCOM8ATYy2l4"
   },
   "outputs": [],
   "source": [
    "print(f\"\\nStarting:  Parsing 'AgreementsLarge' Folder\")\n",
    "msa_lib = Library().create_new_library(\"example6_library\")\n",
    "msa_lib.add_files(agreements_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "VHPA9saYy73o"
   },
   "outputs": [],
   "source": [
    "print(f\"\\nCompleted Parsing - now, let's look for the 'master service agreements', e.g., 'msa'\")\n",
    "q = Query(msa_lib)\n",
    "query = '\"master services agreement\"'\n",
    "results = q.text_search_by_page(query, page_num=1, results_only=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "nh4JVab_zDbo"
   },
   "outputs": [],
   "source": [
    "msa_docs = results[\"file_source\"]\n",
    "msa_doc_ids = results[\"doc_ID\"]\n",
    "prompter = Prompt().load_model(llm)\n",
    "print(\"update: identified the following msa doc id: \", msa_doc_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 211
    },
    "id": "iAmOCEC-zdiI",
    "outputId": "106d0350-7ab1-41e3-9bf4-30961ae5601c"
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'msa_doc_ids' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-46123684ada0>\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdoc_id\u001b[0m \u001b[0;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsa_doc_ids\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m     \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"\\n\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[0mdocs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmsa_docs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msep\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdocs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'msa_doc_ids' is not defined"
     ]
    }
   ],
   "source": [
    "for i, doc_id in enumerate(msa_doc_ids):\n",
    "\n",
    "    print(\"\\n\")\n",
    "    docs = msa_docs[i]\n",
    "    if os.sep in docs:\n",
    "      docs = docs.split(os.sep)[-1]\n",
    "\n",
    "    print (i+1, \"Reviewing MSA - \", doc_id, docs)\n",
    "\n",
    "    doc_filter = {\"doc_ID\": [doc_id]}\n",
    "    termination_provisions = q.text_query_with_document_filter(\"termination\", doc_filter)\n",
    "\n",
    "    sources = prompter.add_source_query_results(termination_provisions)\n",
    "\n",
    "    response = prompter.prompt_with_source(\"What is the notice for termination for convenience?\")\n",
    "\n",
    "    stats = prompter.evidence_comparison_stats(response)\n",
    "    ev_source = prompter.evidence_check_sources(response)\n",
    "\n",
    "    for i, resp in enumerate(response):\n",
    "      print(\"update: llm response - \", resp)\n",
    "      print(\"update: compare with evidence- \", stats[i][\"comparison_stats\"])\n",
    "      print(\"update: sources - \", ev_source[i][\"source_review\"])\n",
    "\n",
    "    prompter.clear_source_materials()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "BSx-KqWizpG4"
   },
   "outputs": [],
   "source": [
    "print(\"\\nupdate: Prompt state saved at: \", os.path.join(LLMWareConfig.get_prompt_path(),prompter.prompt_id))\n",
    "prompter.save_state()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "mrbGQPCb0Ye3"
   },
   "outputs": [],
   "source": [
    "csv_output = HumanInTheLoop(prompter).export_current_interaction_to_csv()\n",
    "print(\"\\nupdate: CSV output for human review - \", csv_output)"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "gpuType": "T4",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
